﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Xml.Serialization;
using System.Collections.ObjectModel;

namespace SetCLib
{
    public class TitleValueItem : INotifyPropertyChanged
    {
        #region Properties

        private string _Title;
        public string Title
        {
            get { return _Title; }
            set
            {
                _Title = value;
                OnPropertyChanged("Title");
            }
        }
        private bool _Checked;
        public bool Checked
        {
            get { return _Checked; }
            set
            {
                _Checked = value;
                OnPropertyChanged("Checked");
            }
        }

        private string _Group;
        public string Group
        {
            get { return _Group; }
            set
            {
                _Group = value;
                OnPropertyChanged("Group");
            }
        }

        private string _UID;
        public string UID
        {
            get { return _UID; }
            set
            {
                _UID = value;
                OnPropertyChanged("UID");
            }
        }

        private bool _IsHeader;
        public bool IsHeader
        {
            get { return _IsHeader; }
            set
            {
                _IsHeader = value;
                OnPropertyChanged("IsHeader");
            }
        }

        #endregion

        #region NotifyChanges

        // Create the OnPropertyChanged method to raise the event
        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }


    public class TitleValueItemCollection : ObservableCollection<TitleValueItem>
    {
        #region Events

        public delegate void ItemCheckedChangedEventHandler(object sender);
        public event ItemCheckedChangedEventHandler ItemCheckedChanged;

        #endregion

        #region Overrides

        protected override void InsertItem(int index, TitleValueItem item)
        {
            item.PropertyChanged += new PropertyChangedEventHandler(item_PropertyChanged);
            base.InsertItem(index, item);
        }

        protected override void RemoveItem(int index)
        {
            if (index > -1 && index < Items.Count)
            {
                Items[index].PropertyChanged -= new PropertyChangedEventHandler(item_PropertyChanged);
            }
            base.RemoveItem(index);
        }

        void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Checked")
            {
                ItemCheckedChanged(sender);
            }
        }

        #endregion
    }
}
